<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="24">
                <el-card shadow="hover">
                    <el-form label-width="90px" :model="param" :rules="rules" ref="passwork" class="ms-content">
                        <el-form-item label="用户名："> {{ name }} </el-form-item>
                        <el-form-item label="旧密码：">
                            <el-input type="password" v-model="param.old"></el-input>
                        </el-form-item>
                        <el-form-item label="新密码：">
                            <el-input type="password" v-model="param.new"></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码：">
                            <el-input type="password" v-model="param.new1"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="onSubmit()">保存</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import md5 from 'crypto-js/md5';

export default {
    name: 'passwork',
    data() {
        return {
            name: '',
            param: {
                old:'',
                new:'',
                new1: '',
            },
            rules: {
                old: [{ required: true, message: '请输入旧密码', trigger: 'blur' }],
                new: [{ required: true, message: '请输入新密码', trigger: 'blur' }],
                new1: [{ required: true, message: '请输入确认密码', trigger: 'blur' }],
            },
        };
    },
    created() {
        this.name = localStorage.getItem('ms_username');
    },
    methods: {
        onSubmit(){
            this.$refs.passwork.validate(valid => {
                console.log(valid);
                if (valid) {

                    let md5PasswordOld = md5(this.param.old).toString();
                    let md5Password = md5(this.param.new).toString();
                    let md5Password1 = md5(this.param.new1).toString();
                    if (md5Password != md5Password1){
                        this.$message.error('两次输入的新密码不同');
                        return false;
                    }
                    let ajaxData = {
                        'id': localStorage.getItem('ms_id'),
                        'name': this.param.username,
                        'password': md5PasswordOld,
                        'newpassword': md5Password,
                    };
                    
                    this.http.post('/admin/updatepwd', ajaxData).then(res => {
                        console.log(res);
                        this.$message.success('修改密码成功');
                        localStorage.removeItem('ms_id');
                        localStorage.removeItem('token');
                        localStorage.removeItem('userInfo');
                        localStorage.removeItem('ms_username');
                        this.$router.push('/login');
                        // localStorage.setItem('token', res.data.token);
                        // localStorage.setItem('userInfo', res.data);
                        // localStorage.setItem('ms_username', res.data.account);
                        // this.$router.push('/');
                    }).catch(err => {
                        console.log(err);
                        this.$message.error(err.msg);
                    })
                    // localStorage.setItem('ms_username', this.param.username);
                    // this.$router.push('/');
                } else {
                    this.$message.error('请输入账号和密码');
                    console.log('error submit!!');
                    return false;
                }
            });
        }
    }
};
</script>

<style scoped>
.info {
    text-align: center;
    padding: 35px 0;
}
.info-image {
    position: relative;
    margin: auto;
    width: 100px;
    height: 100px;
    background: #f8f8f8;
    border: 1px solid #eee;
    border-radius: 50px;
    overflow: hidden;
}
.info-image img {
    width: 100%;
    height: 100%;
}
.info-edit {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    opacity: 0;
    transition: opacity 0.3s ease;
}
.info-edit i {
    color: #eee;
    font-size: 25px;
}
.info-image:hover .info-edit {
    opacity: 1;
}
.info-name {
    margin: 15px 0 10px;
    font-size: 24px;
    font-weight: 500;
    color: #262626;
}
.crop-demo-btn {
    position: relative;
}
.crop-input {
    position: absolute;
    width: 100px;
    height: 40px;
    left: 0;
    top: 0;
    opacity: 0;
    cursor: pointer;
}
</style>